// pages/Member.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  data: {
    navtop:'',
    avatarUrl: defaultAvatarUrl,
    hidden:true,
    lrxx:{},
    jifen:0,
    avatarBase64: '',
    heamimg:""
  },
  onLoad(){
          // 导航栏
      // 1.获取屏幕可使用宽度
      let windowWidth = wx.getSystemInfoSync().windowWidth;
      // 2.获取状态栏高度
      const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
      //3.获取胶囊按钮位置信息
      const menuButton = wx.getMenuButtonBoundingClientRect();
      this.setData({
        navtop:menuButton.top,
      });

  },
  onShow() {
    const that = this;
    that.getphone();
    that.getjifen();
    // 每隔300毫秒执行一次 getphone 和 getjifen 方法
    const intervalId = setInterval(function() {
      that.getphone();
      that.getjifen();
    }, 600);
  
    // 5秒后检查 heamimg 的长度并清除定时器
    setTimeout(function() {
      if (that.data.lrxx.headimg.length>0) {
        clearInterval(intervalId);
      }
    }, 3000);
  },
     //页面跳转
     getinfo(){
      wx.navigateTo({
        url: '../../pages/user/user'
      })
    },
    getinfo2(){
      wx.navigateTo({
        url: '../../pages/oldcard/oldcard'
      })
    },
    getinfo2(){
      wx.navigateTo({
        url: '../../pages/oldcard/oldcard'
      })
    },
    getharvestAddress(){
      wx.navigateTo({
        url: '../../pages/harvestAddress/harvestAddress',
      })
    },
    getuserCheckIn(){
      wx.navigateTo({
        url: '../../pages/userCheckIn/userCheckIn',
      })
    },
    getmembershioCard(){
      wx.navigateTo({
        url: '../../pages/membershipCard/membershipCard',
      })
    },
    getmessageNotification(){
      wx.navigateTo({
        url: '../../pages/messageNotification/messageNotification',
      })
    },
    getharddata(){
      wx.navigateTo({
        url: '../../pages/harddata/harddata',
      })
    },
    getoldmaninformation(){
      wx.navigateTo({
        url: '../../pages/oldmaninformation/oldmaninformation',
      })
    },
    getate(){
      this.setData({
        hidden:!this.data.hidden
      })
    },
    wanshanxinxi(){
        wx.navigateTo({
            url: '../../pages/user/user'
          })
    },
    onChooseAvatar(e) {
      const { avatarUrl } = e.detail 
      this.setData({
        avatarUrl,
      },()=>{
        console.log(this.data.avatarUrl);
        this.downloadAndConvertToBase64(this.data.avatarUrl);
      })
    },

    getphone(){
      const app =getApp()
      const that=this
      wx.request({
        url: 'https://dj.ifankui.com/api/usr/getProfile',
        method:"POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data:{
          openid:app.globalData.openid
        },
        success(res){
          console.log(res);
          that.setData({
             lrxx:res.data.data
          },function(){
            console.log(that.data.lrxx);
            app.globalData.headimg=that.data.lrxx.headimg
          })
          
        }
      })
    },

    getjifen(){
      const that=this
      const app =getApp()
      
        wx.request({
          url: 'https://dj.ifankui.com/api/usr/checkinInfo',
          method:"POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data:{
            openid:app.globalData.openid
          },
          success(res){
              that.setData({
                jifen:res.data.jifen
              })
          }
        })
    },
    downloadAndConvertToBase64: function(url) {
      const that = this;
      const app= getApp();
      wx.getFileSystemManager().readFile({
        filePath: url,
        encoding: 'base64',
        success(res) {
          that.setData({
            avatarBase64: 'data:image/png;base64,' +res.data
          });
          console.log('Base64 data:', res.data);
          wx.request({
            url: 'https://dj.ifankui.com/api/usr/uploadHeadimg',
            method:"POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data:{
              openid:app.globalData.openid,
              base64:that.data.avatarBase64,
            },
            success(res){
              console.log(res);
              that.onShow()
            }
          })
        },
        fail(err) {
          console.error('读取文件失败', err);
        }
      });
    }
  ,

})